﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicAcademy
{
    public partial class frmTransactions : Form
    {
        public frmTransactions()
        {
            InitializeComponent();
        }

        private void RefreshGrid()
        {
            long Dredit, Debit;
            long CreditSum = 0, DebitSum = 0;
            if (long.TryParse(txtCredit.Text, out Dredit) == false)
                Dredit = -1;
            if (long.TryParse(txtDebit.Text, out Debit) == false)
                Debit = -1;
            if (chkContainsSearch.Checked)
                fullTransactionTableAdapter.Fill(musicAcademyDataSet.FullTransaction, Program.academyID, txtName.Text, Dredit, Debit, fromDate.Text, toDate.Text, txtDescription.Text);
            else
                fullTransactionTableAdapter.FillByStarting(musicAcademyDataSet.FullTransaction, Program.academyID, txtName.Text, Dredit, Debit, fromDate.Text, toDate.Text, txtDescription.Text);
            grdTransaction.ClearSelection();

            for (int i = 0; i < grdTransaction.Rows.Count; i++)
            {
                CreditSum += long.Parse(grdTransaction["credit", i].Value.ToString());
                DebitSum += long.Parse(grdTransaction["debit", i].Value.ToString());
            }

            txtDebitSum.Text = DebitSum.ToString();
            txtCreditSum.Text = CreditSum.ToString();
        }

        private void frmTransactions_Load(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            RefreshGrid();
        }

        private void fromDate_Leave(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void toDate_Leave(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtDebit_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtCredit_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void chkContainsSearch_CheckedChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnClearSearch_Click(object sender, EventArgs e)
        {
            fromDate.Text = "";
            toDate.Text = "";
            txtName.Text = "";
            txtDescription.Text = "";
            txtDebit.Text = "";
            txtCredit.Text = "";
        }
    }
}
